package com.zou.controller.backstage.menu;

import com.zou.pojo.MaxMenu;
import com.zou.pojo.MinMenu;
import com.zou.service.MaxMenuService;
import com.zou.service.MinMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/backstage/menu/min")
public class MinController {
    @Autowired
    private MinMenuService minMenuService;
    @Autowired
    private MaxMenuService maxMenuService;

    @GetMapping("/list/{id}")
    public String list(@PathVariable("id") Integer id, MinMenu minMenu, Model model) {
        MaxMenu maxMenu = maxMenuService.querySomeMaxMenuByMaxMenu(new MaxMenu().setMaxId(id)).get(0);
        model.addAttribute("maxMenu", maxMenu);
        List<MinMenu> minMenuList = minMenuService.querySomeMinMenuByMinMenu(minMenu.setMaxId(id));
        model.addAttribute("minMenuList", minMenuList);
        return "/backstage/menu/min/list";
    }

    @PostMapping("/add")
    public @ResponseBody
    Object add(MinMenu minMenu) {
        return minMenuService.addMinMenu(minMenu);
    }

    @PostMapping("/del")
    public @ResponseBody
    Object del(Integer id) {
        return minMenuService.delMinMenuById(id);
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, Model model) {
        MinMenu minMenu = minMenuService.querySomeMinMenuByMinMenu(new MinMenu().setMinId(id)).get(0);
        model.addAttribute("minMenu", minMenu);
        return "backstage/menu/min/edit";
    }

    @PostMapping("/edit")
    public @ResponseBody
    Object edit(MinMenu minMenu) {
        return minMenuService.editMinMenu(minMenu);
    }
}
